package com.hiwan.system.web.component;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hiwan.system.cache.DictCacheService;
import com.hiwan.system.common.base.BaseController;
import com.hiwan.system.pojo.DeptInfo;
import com.hiwan.system.pojo.RoleInfo;
import com.hiwan.system.pojo.UserInfo;
import com.hiwan.system.service.dept.DeptService;
import com.hiwan.system.service.role.RoleService;
import com.hiwan.system.service.user.UserService;
import com.hiwan.system.util.UserCacheUtils;

@Controller
@RequestMapping("/component")
public class ChooseUserController extends BaseController{
	
	@Autowired
	private RoleService roleService;
	@Autowired
	private DeptService deptService;
	@Autowired
	private UserService userService;
	@Autowired
	private DictCacheService dictCacheService;

	
	
	@RequestMapping("/queryUser.do")
	public String queryUser(String _userRole,String _belongArea,String _isSub,String _deptYn,
			String _belongDept,String _userId,String _userName,HttpSession session,ModelMap model) {
		// 查询所有角色
		String[] choosedRoles = _userRole.split(",");
		
		List<RoleInfo> roleList =roleService.findList(new RoleInfo());
		
		List<RoleInfo> chooseRoleList = new ArrayList<RoleInfo>();
		for (int i = 0; i < roleList.size(); i++) {
			for (int j = 0; j < choosedRoles.length; j++) {
				if (choosedRoles[j].equals(roleList.get(i).getRoleId())) {
					chooseRoleList.add(roleList.get(i));
				}
			}
		}
		
		
		if (StringUtils.isEmpty(_belongArea)) {// 所属机构为空时，默认为当前登陆登陆
			_belongArea = UserCacheUtils.getOrgId();
		}
		if (StringUtils.isEmpty(_isSub)) {// 默认不包含（0查询当前机构的用户，1查询当前机构及其下属机构的用户）
			_isSub = "0";
		}
		UserInfo userOrgDeptRole = new UserInfo();
		userOrgDeptRole.setOrgId(_belongArea);
		if (!"no".equals(_deptYn)) {
			userOrgDeptRole.setDeptId(_belongDept);
		}
		userOrgDeptRole.setUserId(_userId);
		userOrgDeptRole.setUserName(_userName);
		/*userOrgDeptRole.setRoleIdstr(_userRole);*/
		userOrgDeptRole.setQueryRole(_userRole);
//		userOrgDeptRole.setRoles(chooseRoleList);
		List<UserInfo> userList = userService.selectUserList(userOrgDeptRole);
		List<UserInfo> userOrgDeptlist = new ArrayList<UserInfo>();
		if (userList != null) {
			for (UserInfo info : userList){
				if (!"no".equals(_deptYn)) {
					info.setDeptId(deptService.getDeptName(info.getDeptId()));
//					List<RoleInfo>  roleIdstr = info.getRoles();
					//String roleIdstr ="";
//					String str = "";
					//String[] userRoles = roleIdstr.split(",");
//					for (int i = 0,len=roleIdstr.size(); i < len; i++) {
//						str += DictCacheUtils.getDictValue(roleIdstr.get(i).getRoleId(),"D00006"
//								) + ",";
//					}
//					info.setRoles(str.substring(0, str.length() - 1));
//					info.setRoles(null);
					
					userOrgDeptlist.add(info);
				} else {
//					String roleIdstr = info.getRoleIdstr();
//					String roleIdstr = info.getUserId();//
//					String str = "";
//					String[] userRoles = roleIdstr.split(",");
//					for (int i = 0; i < userRoles.length; i++) {
//						str += dictCacheService.getDictNameByCode("D00006",
//								userRoles[i], "", "zh_CN") + ",";
//					}
//					info.setRoles(str.substring(0, str.length() - 1));
//					info.setRoles(null);
					
					userOrgDeptlist.add(info);
				}

			}
		}
		model.put("userOrgDeptlist", userOrgDeptlist);		
		model.put("roleList", roleList);
		model.put("_deptYn", "no");
		model.put("chooseRoleList", chooseRoleList);
		model.put("userOrgDeptRole", userOrgDeptRole);
		return "/component/chooseUser";
	}
	
	@RequestMapping("/queryDeptList.do")
	public @ResponseBody List<DeptInfo> queryDeptList(String orgId,ModelMap model) {
		List<DeptInfo> list = new ArrayList<DeptInfo>();
		if (!StringUtils.isEmpty(orgId)) {
			list = deptService.getDeptList(orgId, null, "orgA");
		}
		model.put("list", list);
		return list;
	}

	
	/**
	 * 查询用户 满足条件的子机构用户也可以查询
	 */
	@RequestMapping("/chooseUser.do")
	public void chooseUser(String _belongArea,HttpSession session,String _isSub,String _deptYn,
			String _belongDept,String _userId,String _userName,String _userRole,ModelMap model) {
		if (StringUtils.isEmpty(_belongArea)) {// 所属机构为空时，默认为当前登陆登陆
			_belongArea = UserCacheUtils.getOrgId();
		}
		if (StringUtils.isEmpty(_isSub)) {// 默认不包含（0查询当前机构的用户，1查询当前机构及其下属机构的用户）
			_isSub = "0";
		}
		UserInfo userOrgDeptRole = new UserInfo();
		userOrgDeptRole.setOrgId(_belongArea);
		if (!"no".equals(_deptYn)) {
			userOrgDeptRole.setDeptId(_belongDept);
		}
		userOrgDeptRole.setUserId(_userId);
		userOrgDeptRole.setUserName(_userName);
//		userOrgDeptRole.setRoleIdstr(_userRole);
		List<UserInfo> userList = userService.selectUserList(userOrgDeptRole);
		List<UserInfo> userOrgDeptlist = new ArrayList<UserInfo>();
		if (userList != null) {
			for (UserInfo info : userList) {
				if (!"no".equals(_deptYn)) {
					
//					info.setDeptName(deptService.getDeptName(info.getDeptId()));
//					String roleIdstr = info.getRoleIdstr();
					String str = "";
//					String[] userRoles = roleIdstr.split(",");
					String[] userRoles = {};
					for (int i = 0; i < userRoles.length; i++) {
						str += dictCacheService.getDictNameByCode("D00006",
								userRoles[i], "", "zh_CN") + ",";
					}
//					info.setRoles(str.substring(0, str.length() - 1));
					info.setRoles(null);
					
					userOrgDeptlist.add(info);
				} else {
//					String roleIdstr = info.getRoleIdstr();
					String str = "";
//					String[] userRoles = roleIdstr.split(",");
					String[] userRoles = {};
					for (int i = 0; i < userRoles.length; i++) {
						str += dictCacheService.getDictNameByCode("D00006",
								userRoles[i], "", "zh_CN") + ",";
					}
					info.setRoles(null);
					userOrgDeptlist.add(info);
				}

			}
		}
		model.put("userOrgDeptlist", userOrgDeptlist);
	}


}
